#include "playscene.h"
#include <QMenu>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QThread>
#include <QPropertyAnimation>

// PlayScene::PlayScene(QWidget *parent)
//     : QMainWindow{parent}
// {}

PlayScene::PlayScene(int levelNum)
{
    this->levelIndex = levelNum;

    // 初始化游戏场景
    setFixedSize(320, 588);
    setWindowIcon(QPixmap(":/res/Coin0001.png"));
    setWindowTitle("翻金币场景");

    // 创建菜单栏
    QMenuBar* bar = menuBar();
    setMenuBar(bar);

    // 创建开始菜单
    QMenu* startMenu = bar->addMenu("开始");

    // 创建退出 菜单项
    QAction* quitAction = startMenu->addAction("退出");

    // 点击退出 实现退出游戏
    connect(quitAction, &QAction::triggered, this, [=](){
        close();
    });

    // 返回按钮
    MyPushButton* backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(width() - backBtn->width(), height() - backBtn->width());

    // 点击返回
    connect(backBtn, &MyPushButton::clicked, this, [=](){
        // 告诉主场景 我返回了 主场景监听ChooseLevelScene的返回按钮
        // 延时返回
        QTimer::singleShot(100, this, [=](){
            emit chooseSceneBack();
        });
    });

    // 显示当前关卡号
    QLabel* label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华为新魏");
    label->setText(QString("level: %1").arg(levelNum));
    label->setGeometry(30, height() - 50, 120, 50);
    label->setFont(font);

    dataConfig cfg;
    // 初始化每个关卡的二维数组
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            gameArray[i][j] =  cfg.mData[levelNum][i][j];
        }
    }

    // 显示金币背景图案
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            // 绘制背景图片
            QPixmap pix = QPixmap(":res/BoardNode.png");
            QLabel* label = new QLabel;
            label->setGeometry(0, 0, pix.width(), pix.height());
            label->setParent(this);
            label->setPixmap(pix);
            label->move(57 + i * 50, 200 + j * 50);

            // 创建金币
            QString str = gameArray[i][j] == 1 ? ":/res/Coin0001.png" : ":/res/Coin0008.png";
            MyCoin* coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(57 + i * 50, 200 + j * 50);
            coin->posX = i;
            coin->posY = j;
            coin->flag = gameArray[i][j];
            coinBtn[i][j] = coin;

            // 点击金币 进行翻转
            connect(coin, &MyCoin::clicked, this, [=](){
                coin->changeFlag();
                gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0;

                if (coin->posX + 1 <= 3)
                {
                    coinBtn[coin->posX + 1][coin->posY]->changeFlag();
                    gameArray[coin->posX + 1][coin->posY] = gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0;
                }

                if (coin->posY - 1 >= 0)
                {
                    coinBtn[coin->posX][coin->posY - 1]->changeFlag();
                    gameArray[coin->posX][coin->posY - 1] = gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0;
                }

                if (coin->posX - 1 >= 0)
                {
                    coinBtn[coin->posX - 1][coin->posY]->changeFlag();
                    gameArray[coin->posX - 1][coin->posY] = gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0;
                }

                if (coin->posY + 1 <= 3)
                {
                    coinBtn[coin->posX][coin->posY + 1]->changeFlag();
                    gameArray[coin->posX][coin->posY + 1] = gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0;
                }

                checkWin();
            });
        }
    }
    winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->setGeometry(0, 0, tmpPix.width(), tmpPix.height());
    winLabel->move((width() - tmpPix.width()) * 0.5, -tmpPix.height());
}

void PlayScene::checkWin()
{
    isWin = true;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            if (gameArray[i][j] == 0)
            {
                isWin = false;
                break;
            }
        }}
    if (isWin)
    {
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 4; ++j) {
                coinBtn[i][j]->isWin = true;
            }
        }

        // 创建动态对象
        QPropertyAnimation * animation = new QPropertyAnimation(winLabel, "geometry");
        // 设置动画的时间间隔
        animation->setDuration(1000);

        // 起始位置
        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
        // 结束位置
        animation->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));

        // 设置弹跳曲线
        animation->setEasingCurve(QEasingCurve::OutBounce);

        // 执行动画
        animation->start();
    }
}

void PlayScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    //painter.drawPixmap(0, 0, pix);
    painter.drawPixmap(0, 0, width(), height(), pix);

    // 画背景上图标
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
    painter.drawPixmap(0, 0 , pix);
}
